home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / c01lab4.zip / LRMRDR / LRM_CHAP.ZIP / CHAP12.DOC < prev    next >
Text File  |  1992-04-21  |  42KB  |  1,032 lines

  1. >                              12. Generic Units
  2.  
  3.  
  4. A generic unit is a program unit that is either a generic subprogram  or  a
  5. generic  package.   A generic unit is a template, which is parameterized or
  6. not, and from which corresponding (nongeneric) subprograms or packages  can
  7. be  obtained.   The resulting program units are said to be instances of the
  8. original generic unit.
  9.  
  10.  
  11. A generic unit  is  declared  by  a  generic  declaration.   This  form  of
  12. declaration  has  a  generic  formal  part  declaring  any  generic  formal
  13. parameters.  An instance of a generic unit is obtained as the result  of  a
  14. generic  instantiation  with  appropriate generic actual parameters for the
  15. generic formal parameters.  An  instance  of  a  generic  subprogram  is  a
  16. subprogram.  An instance of a generic package is a package.
  17.  
  18.  
  19. Generic  units are templates.  As templates they do not have the properties
  20. that are specific to their nongeneric counterparts.  For example, a generic
  21. subprogram can be instantiated but it cannot be called.  In  contrast,  the
  22. instance  of  a generic subprogram is a nongeneric subprogram;  hence, this
  23. instance can be called but it cannot be used to produce further  instances.
  24.  
  25.  
  26. References:   declaration  3.1,  generic  actual  parameter  12.3,  generic
  27. declaration 12.1, generic formal parameter 12.1, generic formal part  12.1,
  28. generic  instantiation 12.3, generic package 12.1, generic subprogram 12.1,
  29. instance 12.3, package 7, program unit 6, subprogram 6
  30.  
  31. > 12.1  Generic Declarations
  32.  
  33.  
  34. A generic declaration declares a generic unit, which is  either  a  generic
  35. subprogram  or a generic package.  A generic declaration includes a generic
  36. formal part declaring any generic  formal  parameters.   A  generic  formal
  37. parameter  can  be  an  object;   alternatively  (unlike  a  parameter of a
  38. subprogram), it can be a type or a subprogram.
  39.  
  40.  
  41.     generic_declaration ::= generic_specification;
  42.  
  43.     generic_specification ::=
  44.          generic_formal_part subprogram_specification
  45.        | generic_formal_part package_specification
  46.  
  47.     generic_formal_part ::= generic {generic_parameter_declaration}
  48.  
  49.     generic_parameter_declaration ::=
  50.          identifier_list : [in [out]] type_mark [:= expression];
  51.        | type identifier is generic_type_definition;
  52.        | private_type_declaration
  53.        | with subprogram_specification [is name];
  54.        | with subprogram_specification [is <>];
  55.  
  56.     generic_type_definition ::=
  57.          (<>) | range <> | digits <> | delta <>
  58.        | array_type_definition | access_type_definition
  59.  
  60.  
  61. The terms generic formal object (or simply, formal object), generic  formal
  62. type  (or  simply,  formal type), and generic formal subprogram (or simply,
  63. formal subprogram) are  used  to  refer  to  corresponding  generic  formal
  64. parameters.
  65.  
  66.  
  67. The only form of subtype indication allowed within a generic formal part is
  68. a  type  mark (that is, the subtype indication must not include an explicit
  69. constraint).  The designator of a generic subprogram must be an identifier.
  70.  
  71.  
  72. Outside the specification and body of a generic  unit,  the  name  of  this
  73. program unit denotes the generic unit.  In contrast, within the declarative
  74. region  associated with a generic subprogram, the name of this program unit
  75. denotes the subprogram obtained by the current instantiation of the generic
  76. unit.  Similarly, within the declarative region associated with  a  generic
  77. package,  the name of this program unit denotes the package obtained by the
  78. current instantiation.
  79.  
  80.  
  81. The elaboration of a generic declaration has no other effect.
  82.  
  83.  
  84. Examples of generic formal parts:
  85.  
  86.     generic     --  parameterless
  87.  
  88.     generic
  89.        SIZE : NATURAL;  --  formal object
  90.  
  91.     generic
  92.        LENGTH : INTEGER := 200;           -- formal object with a default expression
  93.        AREA   : INTEGER := LENGTH*LENGTH; -- formal object with a default expression
  94.  
  95.     generic
  96.        type ITEM  is private;                       -- formal type
  97.        type INDEX is (<>);                          -- formal type
  98.        type ROW   is array(INDEX range <>) of ITEM; -- formal type
  99.        with function "<"(X, Y : ITEM) return BOOLEAN;    -- formal subprogram
  100.  
  101.  
  102. Examples of generic declarations declaring generic subprograms:
  103.  
  104.     generic
  105.        type ELEM is private;
  106.     procedure EXCHANGE(U, V : in out ELEM);
  107.  
  108.     generic
  109.        type ITEM is private;
  110.        with function "*"(U, V : ITEM) return ITEM is <>;
  111.     function SQUARING(X : ITEM) return ITEM;
  112.  
  113.  
  114. Example of a generic declaration declaring a generic package:
  115.  
  116.     generic
  117.        type ITEM   is private;
  118.        type VECTOR is array (POSITIVE range <>) of ITEM;
  119.        with function SUM(X, Y : ITEM) return ITEM;
  120.     package ON_VECTORS is
  121.        function SUM  (A, B : VECTOR) return VECTOR;
  122.        function SIGMA(A    : VECTOR) return ITEM;
  123.        LENGTH_ERROR : exception;
  124.     end;
  125.  
  126. Notes:
  127.  
  128.  
  129. Within a generic subprogram, the name of this program unit acts as the name
  130. of a subprogram.  Hence this name can be overloaded, and it can appear in a
  131. recursive call of the current instantiation.  For  the  same  reason,  this
  132. name  cannot  appear  after  the reserved word new in a (recursive) generic
  133. instantiation.
  134.  
  135.  
  136. An expression that occurs in a generic formal part is  either  the  default
  137. expression  for  a generic formal object of mode in, or a constituent of an
  138. entry name given as default name for a formal subprogram,  or  the  default
  139. expression for a parameter of a formal subprogram.  Default expressions for
  140. generic  formal  objects  and default names for formal subprograms are only
  141. evaluated for generic  instantiations  that  use  such  defaults.   Default
  142. expressions  for  parameters  of  formal subprograms are only evaluated for
  143. calls of the  formal  subprograms  that  use  such  defaults.   (The  usual
  144. visibility  rules  apply  to  any  name  used in a default expression:  the
  145. denoted entity must therefore be visible at the place of  the  expression.)
  146.  
  147.  
  148. Neither   generic  formal  parameters  nor  their  attributes  are  allowed
  149. constituents of static expressions (see 4.9).
  150.  
  151.  
  152. References:   access  type  definition  3.8,  array  type  definition  3.6,
  153. attribute   4.1.4,   constraint   3.3,  declaration  3.1,  designator  6.1,
  154. elaboration has no other effect 3.1, entity 3.1, expression  4.4,  function
  155. 6.5,  generic  instantiation  12.3,  identifier  2.3,  identifier list 3.2,
  156. instance  12.3,  name  4.1,  object  3.2,  overloading  6.6  8.7,   package
  157. specification  7.1,  parameter of a subprogram 6.2, private type definition
  158. 7.4, procedure 6.1, reserved word 2.9, static expression 4.9, subprogram 6,
  159. subprogram specification 6.1, subtype indication 3.3.2, type 3.3, type mark
  160. 3.3.2
  161.  
  162. > 12.1.1  Generic Formal Objects
  163.  
  164.  
  165. The first form of generic parameter  declaration  declares  generic  formal
  166. objects.   The type of a generic formal object is the base type of the type
  167. denoted by the type mark given in the  generic  parameter  declaration.   A
  168. generic  parameter  declaration with several identifiers is equivalent to a
  169. sequence of single generic parameter declarations, as explained in  section
  170. 3.2.
  171.  
  172.  
  173. A  generic  formal  object  has a mode that is either in or in out.  In the
  174. absence of an explicit mode indication in a generic parameter  declaration,
  175. the  mode  in  is  assumed;  otherwise the mode is the one indicated.  If a
  176. generic parameter declaration ends with an expression,  the  expression  is
  177. the  default  expression  of  the  generic  formal  parameter.   A  default
  178. expression is only allowed  if  the  mode  is  in  (whether  this  mode  is
  179. indicated explicitly or implicitly).  The type of a default expression must
  180. be that of the corresponding generic formal parameter.
  181.  
  182.  
  183. A  generic  formal object of mode in is a constant whose value is a copy of
  184. the value supplied as the matching generic actual parameter  in  a  generic
  185. instantiation,  as described in section 12.3.  The type of a generic formal
  186. object of mode in must not be a  limited  type;   the  subtype  of  such  a
  187. gener